Repository navigation
Stop Entry.new and the serialisers from mutating the caller's hash - #202
Merged
Merged
Conversation
Entry#initialize took :bibtex_type and :bibtex_key out of the attribute hash with Hash#delete, so the caller's hash lost both keys and a frozen hash raised FrozenError. It now reads the two keys while it walks the hash and adds every other pair as a field, which leaves the argument alone and builds the same entry. Walking the hash directly also skips the copy that #add makes, so parsing 1000 entries allocates 3000 fewer objects. test_construct can pass the frozen RAILS_ATTRIBUTES without a dup.
Both set the default quotes with options[:quotes] ||= ..., which wrote into the caller's hash and raised FrozenError for a frozen one. Bibliography#to_s, #to_a and #to_hash pass their options straight through, so they had the same problem. The entry methods now merge the default into a new hash when :quotes is missing. Bibliography#to_s and #to_a do the same once up front, so the entries see :quotes and do not each copy the hash; serialising 1000 entries allocates three objects more than before, not 3000.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #201, which stopped
Bibliography#save_tofrom changing the options hash passed to it. This PR fixes the same kind of bug inEntry.newand in the serialisers.Bug
Entry#initializeremoved:bibtex_typeand:bibtex_keyfrom the attribute hash withHash#delete, so the caller's hash lost both keys, and a frozen hash raisedFrozenError:test_constructworked around this withRAILS_ATTRIBUTES.dup.Fix
:bibtex_typeand:bibtex_keyto set the type and key, and adds every other pair as a field withValue.create, the same conversion#adduses. The resulting entry is the same as before. Because it no longer calls#add, it also skips the hash copy that#addmakes.test_constructnow passes the frozen constant directly.options[:quotes] ||= %w[{ }]wrote into the caller's hash.Bibliography#to_s,#to_aand#to_hash(and so#to_yamland#to_json) pass their options through to these methods. The entry methods now merge the default into a new hash.Bibliography#to_sand#to_amerge it once up front, so the entries don't each copy the hash.#to_hashnow calls#to_a.Tests cover a frozen hash and an unchanged caller hash, including the type and key keys, for
Entry.new,Entry#to_s,Entry#to_hashandBibliography#to_s,#to_aand#to_hash. Another test checks that the resulting entry's type, key and fields have not changed.Other places checked
No change needed:
Entry#addbuilds a new hash withHash[*arguments.flatten].Bibliography#addand<<with a hash go throughElement.parsetoEntry.new, which this PR fixes.Bibliography.newandLexer.newusedefaults.merge(options).CiteProcConverteruses{ quotes: [] }.merge(options).BibTeXMLConverteronly reads its options.Value#to_susesreject.Namesonly reads its options.RDFConverter#remove_from_fallbackdeletes from its own copy offields.keys.Open question:
Bibliography#to_sand#to_anow pass:quotesto every element, not only to entries. All the other element types ignore their options, so the output is the same.No callers in
lib/,test/orfeatures/depended on the keys being removed.Allocations
Measured with
GC.stat(:total_allocated_objects)on 1000 generated@bookentries with five fields each:BibTeX.parseBibliography#to_sBibliography#to_aEntry.new(type, key, title)Parsing allocates three objects fewer per entry. Serialising allocates two or three more per call, not per entry. Without the up-front merge in
Bibliography, it would have been about 3,000 more per 1000 entries.rake testandcucumberpass on Ruby 3.4.12 and 4.0.6: 916 runs with 0 failures, and 141 of 141 scenarios.rubocopreports no offenses, and.rubocop_todo.ymlis unchanged.